//
//  ViewController.m
//  02-nsoutputstream
//
//  Created by J on 16/2/26.
//  Copyright © 2016年 J. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<NSURLConnectionDelegate>
@property(nonatomic,strong)NSOutputStream* outPutString;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
   NSURLConnection* con =  [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://download.wavetlan.com/SVV/Media/HTTP/H264/Talkinghead_Media/H264_test1_Talkinghead_mp4_480x360.mp4"]] delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    NSString* path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
   self.outPutString = [NSOutputStream outputStreamToFileAtPath:[path stringByAppendingPathComponent:response.suggestedFilename] append:YES];//response.suggestedFilename服务器返回的文件名
    [self.outPutString open];

}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
 
    [self.outPutString write:[data bytes] maxLength:data.length];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    
    [self.outPutString close];
    NSLog(@"---------");
    
}
@end
